/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.mjw.rememome.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.lang.String;
import java.util.ArrayList;
import java.util.List;
import org.me.mjw.rememome.R;
import org.me.mjw.rememome.db.ReMemoMeDbAdapter;
import org.me.mjw.rememome.memo.Memo;

/**
 *
 * @author MWaldman
 */
public class ListActivity extends android.app.ListActivity
{

    private ReMemoMeDbAdapter dbAdapter;

    @Override
    protected void onResume()
    {
        super.onResume();
        refreshList();
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here
        ListView lv;

//        refreshList();

        lv = getListView();
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {

            public void onItemClick(AdapterView<?> av, View v, int position, long id)
            {
                String item = (String) getListView().getItemAtPosition(position);
                Toast.makeText(getApplicationContext(), "Set Properties for: " + item, Toast.LENGTH_LONG).show();
            }
        });
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
        {

            public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id)
            {
                deleteWithWarning(position);
                return true;
            }
        });
    }

//    @Override
//    protected void onListItemClick(ListView l, View v, int position, long id)
//    {
//        super.onListItemClick(l, v, position, id);
//        Toast.makeText(getApplicationContext(), "On List Item Click", Toast.LENGTH_SHORT).show();
//        final String item;
//        AlertDialog deleteDialog;
//        item = (String) getListView().getItemAtPosition(position);
//
//
//        deleteDialog = new AlertDialog.Builder(this).create();
//        deleteDialog.setTitle("Delete Item");
//        deleteDialog.setMessage("Are you sure you want to delete: " + item);
//        deleteDialog.setButton("Yes", new OnClickListener()
//        {
//
//            @Override
//            public void onClick(DialogInterface dialog, int which)
//            {
//                deleteItem(item);
//                refreshList();
//            }
//        });
//        deleteDialog.setButton2("Cancel", new OnClickListener()
//        {
//
//            @Override
//            public void onClick(DialogInterface dialog, int which)
//            {
//                // Do Nothing
//            }
//        });
//        deleteDialog.show();
//    }
    private void deleteWithWarning(int position)
    {
        final String item;
        AlertDialog deleteDialog;
        item = (String) getListView().getItemAtPosition(position);

        deleteDialog = new AlertDialog.Builder(this).create();
        deleteDialog.setTitle("Delete Item");
        deleteDialog.setMessage("Are you sure you want to delete: " + item);
        deleteDialog.setButton("Yes", new OnClickListener()
        {

            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                deleteItem(item);
                refreshList();
            }
        });
        deleteDialog.setButton2("Cancel", new OnClickListener()
        {

            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                // Do Nothing
            }
        });
        deleteDialog.show();

    }

    private void deleteItem(String item)
    {
        dbAdapter = new ReMemoMeDbAdapter(this);
        dbAdapter.open();

        Toast.makeText(this, "Deleted: " + item, Toast.LENGTH_SHORT).show();

        dbAdapter.deleteMemo(item);

        dbAdapter.close();
    }

    private void refreshList()
    {
        ListAdapter listAdapter;
        String[] list;
        String type;

        dbAdapter = new ReMemoMeDbAdapter(this);
        dbAdapter.open();

        type = "";
        list = getList(type);
        listAdapter = new ArrayAdapter<String>(this, R.layout.list_item, list);
        setListAdapter(listAdapter);
        dbAdapter.close();
    }

    // Look up List in database based on type
    private String[] getList(String type)
    {
        String[] list;
        List<String> listObj;
        List<Memo> listMemo;

        listObj = new ArrayList<String>();
//
//        listObj.add("Tooth Paste");
//        listObj.add("Mouth Wash");
//        listObj.add("Walk");
        listMemo = dbAdapter.listMemos();

        for (Memo m : listMemo)
        {
            listObj.add(m.getMemo());
        }

        list = listObj.toArray(new String[0]);

        return list;
    }
}
